<template>
    <div class="list" ref="wrapper">
        <div>
            <div class="area">
                <div class="title border-topbottom">当前城市</div>
                <div class="button-list">
                    <div class="button-wrapper">
                        <div class="button">{{this.currnetCity}}</div>
                    </div>
                </div>
            </div>
            
            <div class="area">
                <div class="title border-topbottom">热门城市</div>
                <div class="button-list">
                    <div class="button-wrapper" v-for="item of this.hotCities" :key="item.id" @click="hanleCityClick(item.name)">
                        <div class="button">{{item.name}}</div>
                    </div>

                </div>
            </div>


            <div class="area" v-for="(item,key) of this.cities" :key="key" :ref="key">
                <div class="title border-topbottom">{{key}}</div>
                <div class="botton-list">
                    <div class="item-list">
                        <div class="item border-bottom" v-for="innerItem of item" :key="innerItem.id" @click="hanleCityClick(innerItem.name)">
                            {{innerItem.name}}
                        </div>
                    </div>
                </div> 
            </div>
        </div>
    </div>
</template>

<style lang="stylus" scoped>
@import '~styles/variable.styl';

.border-topbottom: {
    &:before {
        border-color: #ccc;
    }

    &:after {
        border-color: #ccc;
    }
}

.list {
    top: 1.7rem;
    left: 0;
    right: 0;
    bottom: 0;
    position: absolute;
    overflow: hidden;
}

.title {
    line-height: 0.44rem;
    background-color: #eee;
    padding-left: 0.2rem;
    color: #666;
    font-size: 0.26rem;
}

.button-list {
    padding: 0.1rem;
    overflow: hidden;
    padding-right: 0.6rem;

    .button-wrapper {
        display: inline-block;
        width: 30%;

        .button {
            padding: 0.1rem 0;
            margin: 0.1rem;
            text-align: center;
            border: 0.02rem solid #ccc;
            border-radius: 0.06rem;
        }
    }
}

.item-list {
    font-size: 0.14rem;
    line-height: 0.54rem;
    color: #666;
    padding-left: 0.2rem;
    padding-top: 0.05rem;

    .item {
        border-bottom: 1px solid #ccc;
        padding: 0.1rem;
    }
}
</style>

<script>
import { mapState ,mapMutations   } from "vuex"
import Bscroll from "better-scroll";

export default {
  name: "CityList",
  mounted() {
    this.scroll = new Bscroll(this.$refs.wrapper);
  },
  props: {
    cities: Object,
    hotCities: Array,
    letter: String
  },
  computed:{
      ...mapState({
          currnetCity:"city",      
        //   把city映射给currentCIty
      })
  },                
  methods:{
      hanleCityClick(city){  
        //   复杂做法1
        //   this.$store.dispatch("changeCity",city);
        // 复杂做法二：
        // this.$store.commit("changeCity",city);
        this.changeCity(city)
        this.$router.push("/")


      },
      ...mapMutations  (['changeCity'])
  },
  watch: {
    letter() {
        if(this.letter){
            let element = this.$refs[this.letter][0];
            this.scroll.scrollToElement(element)
        }
    }
  }
};
</script>


